{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Kth Distinct String in an Array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #string #counting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #字符串 #计数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: kthDistinct"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #数组中第 K 个独一无二的字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p><strong>独一无二的字符串</strong>&nbsp;指的是在一个数组中只出现过 <strong>一次</strong>&nbsp;的字符串。</p>\n",
    "\n",
    "<p>给你一个字符串数组&nbsp;<code>arr</code>&nbsp;和一个整数&nbsp;<code>k</code>&nbsp;，请你返回&nbsp;<code>arr</code>&nbsp;中第&nbsp;<code>k</code>&nbsp;个&nbsp;<strong>独一无二的字符串</strong>&nbsp;。如果&nbsp;<strong>少于</strong>&nbsp;<code>k</code>&nbsp;个独一无二的字符串，那么返回&nbsp;<strong>空字符串</strong>&nbsp;<code>\"\"</code>&nbsp;。</p>\n",
    "\n",
    "<p>注意，按照字符串在原数组中的 <strong>顺序</strong>&nbsp;找到第 <code>k</code>&nbsp;个独一无二字符串。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1:</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>arr = [\"d\",\"b\",\"c\",\"b\",\"c\",\"a\"], k = 2\n",
    "<b>输出：</b>\"a\"\n",
    "<strong>解释：</strong>\n",
    "arr 中独一无二字符串包括 \"d\" 和 \"a\"<code>&nbsp;。</code>\n",
    "\"d\" 首先出现，所以它是第 1 个独一无二字符串。\n",
    "\"a\" 第二个出现，所以它是 2 个独一无二字符串。\n",
    "由于 k == 2 ，返回 \"a\" 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2:</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>arr = [\"aaa\",\"aa\",\"a\"], k = 1\n",
    "<b>输出：</b>\"aaa\"\n",
    "<strong>解释：</strong>\n",
    "arr 中所有字符串都是独一无二的，所以返回第 1 个字符串 \"aaa\" 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>arr = [\"a\",\"b\",\"a\"], k = 3\n",
    "<b>输出：</b>\"\"\n",
    "<strong>解释：</strong>\n",
    "唯一一个独一无二字符串是 \"b\" 。由于少于 3 个独一无二字符串，我们返回空字符串 \"\" 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= k &lt;= arr.length &lt;= 1000</code></li>\n",
    "\t<li><code>1 &lt;= arr[i].length &lt;= 5</code></li>\n",
    "\t<li><code>arr[i]</code>&nbsp;只包含小写英文字母。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [kth-distinct-string-in-an-array](https://leetcode.cn/problems/kth-distinct-string-in-an-array/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [kth-distinct-string-in-an-array](https://leetcode.cn/problems/kth-distinct-string-in-an-array/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[\"d\",\"b\",\"c\",\"b\",\"c\",\"a\"]\\n2', '[\"aaa\",\"aa\",\"a\"]\\n1', '[\"a\",\"b\",\"a\"]\\n3']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        words_map = Counter(arr)\n",
    "        for char in arr:\n",
    "            if words_map[char] == 1 and k - 1 == 0:\n",
    "                return char\n",
    "            elif words_map[char] == 1:\n",
    "                k -= 1\n",
    "        return \"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        hashtable = {}\n",
    "        for string in arr:\n",
    "            hashtable[string] = hashtable.get(string, 0) + 1\n",
    "        for string, counter in hashtable.items():\n",
    "            if counter == 1:\n",
    "                k -= 1\n",
    "            if k == 0:\n",
    "                return string\n",
    "        return ''\n",
    "                   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        s_dict = {}\n",
    "        for s in arr:\n",
    "            if s in s_dict:\n",
    "                s_dict[s] += 1\n",
    "            else:\n",
    "                s_dict[s] = 1\n",
    "        cnt = 0\n",
    "        idx = -1\n",
    "        while cnt < k:\n",
    "            idx += 1\n",
    "            if idx >= len(arr):\n",
    "                break\n",
    "            if s_dict[arr[idx]] == 1:\n",
    "                cnt += 1\n",
    "        if idx < len(arr):\n",
    "            return arr[idx]\n",
    "        else:\n",
    "            return \"\"\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        dic = {}\n",
    "        for i in arr:\n",
    "            dic[i] = dic.get(i, 0) + 1\n",
    "        l = list(dict(filter(lambda l:l[1] == 1, dic.items())).keys())\n",
    "        if len(l) >= k:\n",
    "            return l[k-1]\n",
    "\n",
    "        else:\n",
    "            return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        freq = Counter(arr)\n",
    "        for s in arr:\n",
    "            if freq[s] == 1:\n",
    "                k -=1\n",
    "                if k==0:\n",
    "                    return s\n",
    "        return \"\"   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        c=collections.Counter(arr)\n",
    "        l=[i for i in c if c[i]==1]\n",
    "        if k<=len(l):\n",
    "            return l[k-1] \n",
    "        else: return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        m = Counter(arr)\n",
    "        for c in m:\n",
    "            if m[c] == 1:\n",
    "                k -= 1\n",
    "            if k == 0:\n",
    "                return c\n",
    "        return ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        dic = {}\n",
    "        for i in arr:\n",
    "            dic[i] = dic.get(i, 0) + 1\n",
    "        l = list(dict(filter(lambda l:l[1] == 1, dic.items())).keys())\n",
    "        if len(l) >= k:\n",
    "            return l[k-1]\n",
    "\n",
    "        else:\n",
    "            return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        count = Counter(arr)\n",
    "        for s,i in count.items():\n",
    "            if i == 1:\n",
    "                if k == 1:\n",
    "                    return s\n",
    "                k -= 1\n",
    "        return \"\"\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        a=[]\n",
    "        for i in arr:\n",
    "          if arr.count(i)==1:\n",
    "              a.append(i)\n",
    "        if len(a)>=k:\n",
    "            return a[k-1]\n",
    "        else:\n",
    "            return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        freq = Counter(arr)\n",
    "        for s in arr:\n",
    "            if freq[s] == 1:\n",
    "                k -= 1\n",
    "                if k == 0:\n",
    "                    return s\n",
    "        return ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        res = []\n",
    "        for i in arr:\n",
    "            if arr.count(i) == 1:\n",
    "                res.append(i)\n",
    "        try:\n",
    "            return res[k-1]\n",
    "        except:\n",
    "            return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        DiffS = self.DiffList(arr)\n",
    "        j = 0\n",
    "        ind = k\n",
    "\n",
    "        for i in arr:\n",
    "            if i in DiffS:\n",
    "                ind += 1\n",
    "            else:\n",
    "                j += 1\n",
    "            if j==k:\n",
    "                return arr[ind-1]\n",
    "        print(\"没有这个\")\n",
    "        return ''\n",
    "        \n",
    "\n",
    "        \n",
    "    def DiffList(self,arr:list[str])->int:\n",
    "        DL = []\n",
    "        for i in range(1,len(arr)):\n",
    "            if arr[i] in arr[:i] and arr[i] not in DL:\n",
    "                DL.append(arr[i])\n",
    "        return DL"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        ds = Counter(arr)\n",
    "        ns = [k for k,v in ds.items() if v == 1]\n",
    "        if len(ns) < k:\n",
    "            return ''\n",
    "        return ns[k-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        count = 0\n",
    "        hax = Counter(arr)\n",
    "\n",
    "        for x in hax:\n",
    "            if hax[x] == 1:\n",
    "                count += 1\n",
    "            if count == k:\n",
    "                return x\n",
    "        return ''\n",
    "        \n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        # 维护 arr 中每个字符串的频数\n",
    "        freq = Counter(arr)\n",
    "        # 遍历 arr 寻找第 k 个独一无二的字符串\n",
    "        for s in arr:\n",
    "            if freq[s] == 1:\n",
    "                k -= 1\n",
    "                if k == 0:\n",
    "                    return s\n",
    "        return \"\"\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        cnt=Counter(arr)\n",
    "        for i in arr:\n",
    "            if cnt[i]==1:\n",
    "                k-=1\n",
    "            if k==0:\n",
    "                return i\n",
    "        return ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        frequency_check = {}\n",
    "        for letter in arr:\n",
    "            if letter in frequency_check:\n",
    "                frequency_check[letter] += 1\n",
    "            else:\n",
    "                frequency_check[letter] = 1\n",
    "        only_once_letter = [temp_key for temp_key in frequency_check.keys() if frequency_check[temp_key] == 1]\n",
    "        try:\n",
    "            return only_once_letter[k-1]\n",
    "        except:\n",
    "            return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        cour=dict(Counter(arr))\n",
    "        res=[]\n",
    "        for a,b in cour.items():\n",
    "            if b==1:\n",
    "                res.append(a)\n",
    "        if len(res)<k:\n",
    "            return \"\"\n",
    "\n",
    "        else:\n",
    "            return res[k-1]\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        d = defaultdict(int)\n",
    "        res = []\n",
    "        for s in arr:\n",
    "            if s not in d: res.append(s)\n",
    "            d[s] += 1\n",
    "        idx = 0\n",
    "        while idx < len(res):\n",
    "            if d[res[idx]] == 1: k -= 1\n",
    "            if k == 0: return res[idx]\n",
    "            idx += 1\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        # Method 1\n",
    "        result = [i for i in arr if arr.count(i) == 1]\n",
    "        return result[k-1] if k <= len(result) else \"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        f=Counter(arr)\n",
    "        for i in arr:\n",
    "           if f[i]==1:\n",
    "                k-=1\n",
    "                if k==0:\n",
    "                    return i\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        str_dict = dict()\n",
    "\n",
    "        for s in arr:\n",
    "            if s not in str_dict:\n",
    "                str_dict[s] = 1\n",
    "            else:\n",
    "                str_dict[s] += 1\n",
    "\n",
    "        cnt = 0\n",
    "        for key, value in str_dict.items():\n",
    "            if value == 1:\n",
    "                cnt += 1\n",
    "                if cnt == k:\n",
    "                    return key\n",
    "        return \"\"\n",
    "                \n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        ad={}\n",
    "        for i in arr:\n",
    "            ad[i]=ad.get(i,0)+1\n",
    "        r=[k for k in ad if ad[k]==1]\n",
    "        #r.sort()\n",
    "        if k>len(r):\n",
    "            return \"\"\n",
    "        else:\n",
    "            return r[k-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        counter=dict()\n",
    "        n=len(arr)\n",
    "        for i in range(n):\n",
    "            counter[arr[i]]=counter.get(arr[i],0)+1\n",
    "        res=[]\n",
    "        for key in counter:\n",
    "            if counter[key]==1:\n",
    "                res+=[key]\n",
    "        if k>len(res):\n",
    "            return ''\n",
    "        else:\n",
    "            return res[k-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: list[str], k: int) -> str:\n",
    "        n = 0\n",
    "        for i in range(len(arr)):\n",
    "            if arr.count(arr[i]) == 1:\n",
    "                n += 1\n",
    "                if n == k:\n",
    "                    return arr[i]\n",
    "        return \"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        c = dict()\n",
    "        for s in arr:\n",
    "            v = c.get(s)\n",
    "            if not v:\n",
    "                c[s] = 1\n",
    "            else:\n",
    "                c[s] = v+1\n",
    "\n",
    "        for s in arr:\n",
    "            v = c.get(s)\n",
    "            if v == 1:\n",
    "                if k == 1:\n",
    "                    return s\n",
    "                else:\n",
    "                    k -= 1\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        c=collections.Counter(arr)\n",
    "        l=[i for i in c if c[i]==1]\n",
    "        if k<=len(l):\n",
    "            return l[k-1] \n",
    "        else: return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        # 维护 arr 中每个字符串的频数\n",
    "        freq = Counter(arr)\n",
    "        # 遍历 arr 寻找第 k 个独一无二的字符串\n",
    "        for s in arr:\n",
    "            if freq[s] == 1:\n",
    "                k -= 1\n",
    "                if k == 0:\n",
    "                    return s\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        result = [i for i in arr if arr.count(i) == 1]\n",
    "        return result[k-1] if k <= len(result) else \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        freq = Counter(arr)\n",
    "        ans = [\"\"] * k \n",
    "        pos = 0\n",
    "        for key, v in freq.items():\n",
    "            if v == 1:\n",
    "                ans[pos] = key\n",
    "                pos += 1\n",
    "            if pos == k:\n",
    "                break\n",
    "        return ans[k-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        # print(arr.count(\"a\"),arr.count('b'))\n",
    "        res = []\n",
    "        for i in arr:\n",
    "            if arr.count(i) == 1:\n",
    "                k -= 1\n",
    "                if k == 0:\n",
    "                    return i\n",
    "        if k != 0:\n",
    "            return \"\"\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        freq=Counter(arr)\n",
    "        for s in arr:\n",
    "            if freq[s]==1:\n",
    "                k-=1\n",
    "                if k==0:\n",
    "                    return s\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        counter = Counter(arr)\n",
    "        i = 0\n",
    "        for s in arr:\n",
    "            if counter[s] == 1:\n",
    "                i += 1\n",
    "            if i == k:\n",
    "                return s\n",
    "        return ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        lst = [c for c, v in Counter(arr).items() if v == 1]\n",
    "        return \"\" if k > len(lst) else lst[k - 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        dic = Counter(arr)\n",
    "        lst = []\n",
    "        for s, v in dic.items():\n",
    "            if v == 1:\n",
    "                lst.append(s)\n",
    "        c = 0\n",
    "        for s in arr:\n",
    "            if s in lst:\n",
    "                c += 1\n",
    "            if c == k:\n",
    "                return s\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        lst =[k for k,v in Counter(arr).items() if v==1]\n",
    "        return \"\" if len(lst)<k else lst[k-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        for data in arr:\n",
    "            if arr.count(data) == 1:\n",
    "                k -= 1\n",
    "            if k == 0:\n",
    "                return data\n",
    "        return \"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "       li = [i for i,c in Counter(arr).items() if c == 1]\n",
    "       return '' if len(li) < k else li[k-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        dic={}\n",
    "        for i in arr:\n",
    "            dic[i]=0\n",
    "        for i in arr:\n",
    "            dic[i]+=1\n",
    "        cnt=[]#用来储存独一无二的字符\n",
    "        for i in dic:\n",
    "            if dic[i]==1:\n",
    "                cnt.append(i)\n",
    "        if len(cnt)>k-1:\n",
    "            return cnt[k-1]\n",
    "        else:\n",
    "            return ''\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        str_dict = Counter(arr)\n",
    "\n",
    "        cnt = 0\n",
    "        for key, value in str_dict.items():\n",
    "            if value == 1:\n",
    "                cnt += 1\n",
    "                if cnt == k:\n",
    "                    return key\n",
    "        return \"\"\n",
    "                \n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        vis = defaultdict(int)\n",
    "        for s in arr:\n",
    "            vis[s] += 1\n",
    "        for s in arr:\n",
    "            if vis[s] == 1:\n",
    "                k -= 1\n",
    "                if k == 0:\n",
    "                    return s\n",
    "\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "#使用函数Counter，可以迅速获取list中每个元素出现的次数\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        words_map = Counter(arr)\n",
    "        count = 0\n",
    "        for char in arr:\n",
    "            if words_map[char] == 1:\n",
    "                count += 1\n",
    "            if count == k:\n",
    "                return char\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        for data in arr:\n",
    "            if arr.count(data) == 1:\n",
    "                k -= 1\n",
    "            if k == 0:\n",
    "                return data\n",
    "        return \"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        cnter = Counter(arr)\n",
    "        cnt = 0\n",
    "        for s in arr:\n",
    "            if cnter[s] == 1:\n",
    "                cnt += 1\n",
    "            if cnt == k:\n",
    "                return s\n",
    "        \n",
    "        return ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        count = 0\n",
    "        hax = Counter(arr)\n",
    "\n",
    "        for x in hax:\n",
    "            if hax[x] == 1:\n",
    "                count += 1\n",
    "            if count == k:\n",
    "                return x\n",
    "        return ''\n",
    "        \n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        for s in arr:\n",
    "            if arr.count(s) == 1:\n",
    "                k -= 1\n",
    "            if k == 0:\n",
    "                return s\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        # 维护 arr 中每个字符串的频数\n",
    "        freq = Counter(arr)\n",
    "        # 遍历 arr 寻找第 k 个独一无二的字符串\n",
    "        for s in arr:\n",
    "            if freq[s] == 1:\n",
    "                k -= 1\n",
    "                if k == 0:\n",
    "                    return s\n",
    "        return \"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        freq = Counter(arr)\n",
    "        try:\n",
    "            return list(filter(lambda x: freq[x] == 1, arr))[k - 1] \n",
    "        except IndexError:\n",
    "            return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        freq = Counter(arr)\n",
    "        for s in arr:\n",
    "            if freq[s]==1:\n",
    "                k -= 1\n",
    "                if k==0:\n",
    "                    return s\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        freq = Counter(arr)\n",
    "        for s in arr:\n",
    "            if freq[s] == 1:\n",
    "                k -= 1\n",
    "                if k == 0:\n",
    "                    return s\n",
    "        return''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        freq = Counter(arr)\n",
    "        for s in arr:\n",
    "            if freq[s]==1:\n",
    "                k-=1\n",
    "                if k==0:\n",
    "                    return s\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        字典 = dict()\n",
    "        for i in arr:\n",
    "            if i in 字典:\n",
    "                字典[i] = 0\n",
    "            else:\n",
    "                字典[i] = 1\n",
    "        返回字符串 = \"\"\n",
    "        k -= 1\n",
    "        for i in 字典:\n",
    "            if 字典[i]:\n",
    "                if k:\n",
    "                    k -= 1\n",
    "                else:\n",
    "                    返回字符串 = i\n",
    "                    break\n",
    "        return  返回字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        num_freq = collections.Counter(arr)\n",
    "        nums = []\n",
    "        for x in arr:\n",
    "            if num_freq[x] == 1:\n",
    "                nums.append(x)\n",
    "        return '' if len(nums) < k else nums[k - 1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        lst = [c for c, v in Counter(arr).items() if v == 1]\n",
    "        return \"\" if k > len(lst) else lst[k - 1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        for data in arr:\n",
    "            if arr.count(data) == 1:\n",
    "                k -= 1\n",
    "            if k == 0:\n",
    "                return data\n",
    "        return \"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        HashMap = collections.defaultdict(int)\n",
    "        for ch in arr:\n",
    "            HashMap[ch] += 1\n",
    "        for ch in arr:\n",
    "            if HashMap[ch] == 1:\n",
    "                if k > 1:\n",
    "                    k -= 1\n",
    "                else:\n",
    "                    return ch\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        dic = Counter(arr)\n",
    "        for s in arr:\n",
    "            if dic[s] == 1:\n",
    "                k -= 1\n",
    "            if k == 0:\n",
    "                return s\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        res = \"\"\n",
    "        cnt =  Counter(arr)\n",
    "\n",
    "        for i,v in cnt.items():\n",
    "            if v == 1: \n",
    "                k -= 1\n",
    "                if k==0:\n",
    "                    res = i\n",
    "                    break\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        res=[]\n",
    "        already=[]\n",
    "        for i in arr:\n",
    "            if i in res:\n",
    "                res.remove(i)\n",
    "                already.append(i)\n",
    "            elif i in already:\n",
    "                continue\n",
    "            else:\n",
    "                res.append(i)\n",
    "        print(res)\n",
    "        return res[k-1] if len(res)>=k else \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        cnt_map=Counter(arr)\n",
    "        res=[]\n",
    "        for i in arr:\n",
    "            if cnt_map[i]==1:\n",
    "                res.append(i)\n",
    "\n",
    "        print(res)\n",
    "        return res[k-1] if len(res)>=k else \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        unique = []\n",
    "        for i in arr:\n",
    "            if arr.count(i) == 1:\n",
    "                unique.append(i)\n",
    "        return \"\" if len(unique) < k else unique[k - 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        c=Counter(arr)\n",
    "        l=[k for k,v in c.items() if v==1]\n",
    "        try:\n",
    "            return l[k-1]\n",
    "        except:\n",
    "            return ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        # 维护 arr 中每个字符串的频数\n",
    "        freq = Counter(arr)\n",
    "        # 遍历 arr 寻找第 k 个独一无二的字符串\n",
    "        for s in arr:\n",
    "            if freq[s] == 1:\n",
    "                k -= 1\n",
    "                if k == 0:\n",
    "                    return s\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        dic = Counter(arr)\n",
    "        for i in arr:\n",
    "            if dic[i] == 1:\n",
    "                k -= 1\n",
    "                if k == 0:\n",
    "                    return i\n",
    "        return ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        c=0\n",
    "        for i in range(len(arr)):\n",
    "            if arr.count(arr[i])==1:\n",
    "                c+=1\n",
    "            if c==k:\n",
    "                return arr[i]\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        cnter = Counter(arr)\n",
    "        res = []\n",
    "        for s in arr:\n",
    "            if cnter[s] == 1:\n",
    "                res.append(s)\n",
    "\n",
    "        return res[k-1] if k <= len(res) else ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        frequency_check = {}\n",
    "        for letter in arr:\n",
    "            if letter in frequency_check:\n",
    "                frequency_check[letter] += 1\n",
    "            else:\n",
    "                frequency_check[letter] = 1\n",
    "        only_once_letter = [temp_key for temp_key in frequency_check.keys() if frequency_check[temp_key] == 1]\n",
    "        try:\n",
    "            return only_once_letter[k-1]\n",
    "        except:\n",
    "            return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        dic = Counter(arr)\n",
    "        for st in arr:\n",
    "            if dic[st] == 1:\n",
    "                k -= 1\n",
    "                if k == 0:\n",
    "                    return st \n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        cnt = Counter(arr)\n",
    "        for s in arr:\n",
    "            if cnt[s] == 1:\n",
    "                k -= 1\n",
    "            if k == 0:\n",
    "                return s\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        # dic=Counter(arr)\n",
    "        # print(dic)\n",
    "        dicc={}\n",
    "        for ar in arr:\n",
    "            dicc[ar]=dicc.get(ar,0)+1\n",
    "        # print(dicc)\n",
    "        num=0\n",
    "        for key,value in dicc.items():\n",
    "            if value==1:\n",
    "                # 要找第 k 个独一无二的，就是第 k 个出现次数为1 的，那么先看出现一次的字符串有几个，小于 k 就是空。\n",
    "                num+=1\n",
    "                if num==k:\n",
    "                    return key\n",
    "        if num<k:\n",
    "            return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        cnt=Counter(arr)\n",
    "        cnt=set([kk for kk,v in cnt.items() if v==1])\n",
    "        c=0\n",
    "        for ch in arr:\n",
    "            c+=(ch in cnt)\n",
    "            if c==k:\n",
    "                return ch \n",
    "        return ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def kthDistinct(self, arr: List[str], k: int) -> str:\n",
    "        cnter = Counter()\n",
    "        for s in arr:\n",
    "            cnter[s] += 1\n",
    "        cnt = 0\n",
    "        for s in arr:\n",
    "            if cnter[s] == 1:\n",
    "                cnt += 1\n",
    "            if cnt == k:\n",
    "                return s\n",
    "        \n",
    "        return ''"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
